<template>
	<div class="padding-across-10" v-loading="loading" element-loading-text="拼命加载中">
		<headerOpreate style="width:300px;padding-bottom: 5px;float: left;" :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
		<div class="block" style="text-align: right;float: right;padding-bottom: 2px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
		<el-table border style="width: 100%" @selection-change="handleSelectionChange" :data="tableData">
			<el-table-column type="selection" width="55" align="center"></el-table-column>
			<!-- <el-table-column type="index" width="55" align="center"></el-table-column> -->
			<el-table-column :label="$t('message.name')" align="center" prop="Name"></el-table-column>
			<el-table-column :label="$t('message.creater')" align="center" prop="CreatedBy"></el-table-column>
			<el-table-column label="创建时间" align="center" prop="CreatedOn"></el-table-column>
			<el-table-column :label="$t('message.operation')" align="center">
				<template slot-scope="scope">
					<el-button type="primary" size="small" @click="editSchedule(scope.row)"><i class="el-icon-edit el-icon--left"></i>{{$t('message.edit')}}</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align:right;margin-bottom:60px;padding-top: 2px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import headerNave from '../../../../../components/headerNave'
	import { GloabFun, HttpServer } from '../../../../../assets/js/common.js'
	import headerOpreate from '../../../../../components/headerOpreate'
	export default {
		components: {
			headerNave,
			headerOpreate
		},
		data() {
			return {
				loading: false,
				loading2: false,
				tableData: [],
				computerDatas: [],
				multipleSelection: [],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
				datas: [],
				editForm: {

				},
				opreateDatas: [{
						show: true,
						size: "small",
						type: 'primary',
						icon: 'el-icon-eleg-icon_refresh el-icon--left',
						name: '刷新',
						action: 'reloadDatas'
					},
					{
						show: GloabFun.getUid() == 'admin' ? true : false,
						type: "primary",
						size: "small",
						icon: "el-icon-plus",
						name: this.$t('message.add'),
						action: "addSchedules"
					},
					{
						show: GloabFun.getUid() == 'admin' ? true : false,
						type: "danger",
						size: "small",
						icon: "el-icon-delete",
						name: this.$t('message.delete'),
						action: "deleteSchedules"
					}
				]
			}
		},
		props: {
			subArgs: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			editSchedule(row) {
				row.currentPage = this.currentPage;
				row.pageSize = this.pageSize;
				this.$emit('changeView', 'EditIntelligenceTimeClose', row);
			},
			addSchedules() {
				this.$emit('changeView', 'AddIntelligenceTimeClose', {});
			},
			deleteSchedules() {
				if(this.formateSelection().length > 0) {
					this.$confirm(this.$t('message.bulkDeleteWarning'), this.$t('message.warning'), {
						confirmButtonText: this.$t('message.yes'),
						cancelButtonText: this.$t('message.no'),
						type: 'warning'
					}).then(() => {
						//批量删除成功处理
						var param = {
							ids: this.formateSelection(),
							UserId: parseInt(GloabFun.getKeyId())
						}
						this.loading = true;
						HttpServer.httpPost.call(this, '/Api/Schedules/Delete', param, function(response) {}, function(response) {
							this.loading = false;
							if(response.data.code === 0) {
								this.getDatas();
								this.$notify({
									title: '成功',
									message: this.$t('message.bulkDeleteSuccess'),
									type: 'success',
									duration: 2500
								});
							} else {
								this.$notify.error({
									title: '错误',
									message: response.data.msg,
									duration: 2500
								});
							}
						});
					}).catch(() => {
						//取消批量删除处理
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					});
				} else {
					this.$notify({
						title: this.$t('message.warning'),
						message: this.$t('message.selectBulkDeleteWarning'),
						type: 'warning',
						duration: 2500
					});
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			formateSelection() {
				var result = [],
					array = this.multipleSelection;
				array.forEach(function(item) {
					result.push(item.ScheduleId);
				})
				return result;
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			//刷新数据
			reloadDatas() {
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/Schedules?page=' + this.currentPage + '&size=' + this.pageSize + '&filter=&access_token=' + GloabFun.getToken()+'&UserId='+parseInt(GloabFun.getKeyId()), function(response) {}, function(response) {
					this.loading = false;
					if(response.data.code === 0) {
						this.tableData = response.data.data;
						this.totalSize = response.data.total;
						this.$notify({
							title: '成功',
							message: this.$t('message.refreshSuccess'),
							type: 'success',
							duration: 2500
						});
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			},
			//去掉时间中的T
			formateLocationData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].CreatedOn.indexOf("T");
					array[i].CreatedOn = array[i].CreatedOn.substring(0, index1) + " " + array[i].CreatedOn.substring(index1 + 1, index1 + 9);
				}
			},
			//数据初始化
			getDatas() {
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/Schedules?page=' + this.currentPage + '&size=' + this.pageSize + '&filter=&access_token=' + GloabFun.getToken()+'&UserId='+parseInt(GloabFun.getKeyId()), function(response) {}, function(response) {
					// console.log('计划任务列表',response)
					this.loading = false;
					if(response.data.code === 0) {
						this.formateLocationData(response.data.data);
						this.tableData = response.data.data;
						this.totalSize = response.data.total;
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			}
		},
		created() {
			this.getDatas();
		}
	}
</script>
<style lang="less">
	.schedule-task {
		.tree_list {
			.el-tree {
				overflow: auto;
			}
			.el-tree-node__expand-icon {
				display: none;
			}
			.el-checkbox {
				margin-left: 10px;
			}
		}
	}
	
	.edit-mission {
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 380px;
			max-height: 480px;
		}
	}
</style>
<style scoped lang="less">
	.tree_list {
		width: 220px;
		float: left;
		height: 100%;
		>div {
			width: 100%;
			height: e("calc(100% - 20px)");
		}
	}
	
	.container {
		width: e("calc(100% - 220px)");
		float: left;
		height: 100%;
		.container-section {
			border: 1px solid #d3dce6;
			height: e("calc(100% - 20px)");
			margin-left: 10px;
			overflow-y: auto;
			form {
				margin: 20px 0 10px 0;
			}
		}
	}
	
	.el-tree-node__expand-icon.is-leaf {
		width: 10px;
		margin: 0;
	}
</style>